﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DB_Test.ServiceReference1;

namespace DB_Test
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        #region Commands
        public static RoutedCommand CencelInputCommand = new RoutedCommand();
		public static RoutedCommand AddProductCommand = new RoutedCommand();
		public static RoutedCommand AddOrderCommand = new RoutedCommand();

        private void CencelInputCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = (xTabCtrl.SelectedIndex == 0 && xProductViewPanel.Visibility == Visibility.Visible) ||
                            (xTabCtrl.SelectedIndex == 1 && xOrderViewPanel.Visibility == Visibility.Visible);
        }

        private void CencelInputCommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            switch (xTabCtrl.SelectedIndex)
            {
                case 0:
                    AllProductsButton_Click(null, new RoutedEventArgs());
                    break;
                case 1:
                    AllOrdersButton_Click(null, new RoutedEventArgs());
                    break;
            }
        }

		private void AddProductCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
		}

		private void AddProductCommandExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			xTabCtrl.SelectedIndex = 0;
			AddProductButton_Click(null, new RoutedEventArgs());
		}

		private void AddOrderCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			e.CanExecute = true;
		}

		private void AddOrderCommandExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			xTabCtrl.SelectedIndex = 1;
            int selectedProduct = e.Parameter == null ? -1 : (int)e.Parameter;
			xOrderViewPanel.SelectedProductID =  selectedProduct;
            AddOrdersButton_Click(e.Parameter, new RoutedEventArgs());
		}

        #endregion
        
        #region Private members
        private WCFServiceClient client;
        #endregion

        #region Initialization
        public Window1()
        {
            InitializeComponent();
			Loaded += new RoutedEventHandler(Window1_Loaded);
        }
        #endregion

        #region Implementation
        private void Window1_Loaded(object sender, RoutedEventArgs e)
		{
			client = new WCFServiceClient();

			try
			{
				client.Open();
				
				UpdateProductsTable(client);
				UpdateOrdersTable(client);
				UpdateCustomersTable(client);
			}
			finally
			{
				client.Close();
			}
        }

        #region Products
        private void AddProductButton_Click(object sender, RoutedEventArgs e)
        {
            xAllProductsBtn.Visibility = Visibility.Visible;
            xAddEditProductPanel.Visibility = Visibility.Collapsed;

            xProductViewPanel.Visibility = Visibility.Visible;
            xProductsList.Visibility = Visibility.Collapsed;

			xProductViewPanel.IsEditing = false;
        }

        private void EditProductButton_Click(object sender, RoutedEventArgs e)
        {
			if(xProductsList.SelectedItem == null)
			{
				MessageBox.Show("Please, select an item!", "", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
				return;
			}

            xAllProductsBtn.Visibility = Visibility.Visible;
            xAddEditProductPanel.Visibility = Visibility.Collapsed;

            xProductViewPanel.Visibility = Visibility.Visible;
            xProductsList.Visibility = Visibility.Collapsed;

			xProductViewPanel.IsEditing = true;
        }

        private void DeleteProductButton_Click(object sender, RoutedEventArgs e)
        {
            if (xProductsList.SelectedItem == null)
            {
                MessageBox.Show("Please, select an item!", "", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
                return;
            }

			ProductsClass product = (ProductsClass)xProductsList.SelectedItem;
			client = new WCFServiceClient();
			try
			{
				client.Open();
				
				if (client.GetIfProductOrdersExists(product.ProductName))
				{
					if (MessageBox.Show("There are some orders on this product. Do you want to continue?", "",
						MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
					{
						return;
					}
				}

				client.DeleteProduct(product);
				
				UpdateProductsTable(client);
				UpdateOrdersTable(client);
				xOrderViewPanel.UpdatePanelInfo(client);
				xCustomersViewPanel.UpdatePanelInfo(client);
			}
			finally
			{
				client.Close();
			}
        }

        private void AllProductsButton_Click(object sender, RoutedEventArgs e)
        {
            xAllProductsBtn.Visibility = Visibility.Collapsed;
            xAddEditProductPanel.Visibility = Visibility.Visible;

			xProductViewPanel.ClearInputExt();
            xProductViewPanel.Visibility = Visibility.Collapsed;
            xProductsList.Visibility = Visibility.Visible;

			if (!xProductViewPanel.ShoudUpdateDB)
				return;

			UpdateProductsTable();
        }

		private void UpdateProductsTable()
		{
			client = new WCFServiceClient();
			try
			{
				client.Open();
				UpdateProductsTable(client);
			}
			finally
			{
				client.Close();
			}
		}

		private void UpdateProductsTable(WCFServiceClient client)
		{
			xProducts.DataContext = client.GetProducts();
        }
        #endregion

        #region Orders
        private void AddOrdersButton_Click(object sender, RoutedEventArgs e)
        {
            xAllOrdersBtn.Visibility = Visibility.Visible;
            xAddEditOrderPanel.Visibility = Visibility.Collapsed;

            xOrderViewPanel.Visibility = Visibility.Visible;
            xOrdersList.Visibility = Visibility.Collapsed;

            xOrderViewPanel.ShoudUpdateDB = false;
        }

        private void DeleteOrdersButton_Click(object sender, RoutedEventArgs e)
        {
            if (xOrdersList.SelectedItem == null)
            {
                MessageBox.Show("Please, select an item!", "", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
                return;
            }
			            
            client = new WCFServiceClient();
            try
            {
                client.Open();

				ProductsOnOrderFullInfoClass fullInfo = (ProductsOnOrderFullInfoClass)xOrdersList.SelectedItem;

				OrdersClass order = client.GetOrderByID(fullInfo.OrderID);
				client.DeleteOrder(order);
								
				UpdateOrdersTable(client);
				UpdateCustomersTable(client);
				xOrderViewPanel.UpdateCustomers();
            }
            finally
            {
                client.Close();
            }

            xOrderViewPanel.ShoudUpdateDB = true;
        }

        private void AllOrdersButton_Click(object sender, RoutedEventArgs e)
        {
            xAllOrdersBtn.Visibility = Visibility.Collapsed;
            xAddEditOrderPanel.Visibility = Visibility.Visible;

			xOrderViewPanel.ClearInputExt();
            xOrderViewPanel.Visibility = Visibility.Collapsed;
            xOrdersList.Visibility = Visibility.Visible;

            if (!xOrderViewPanel.ShoudUpdateDB)
                return;

            UpdateOrdersTable();
        }

        private void UpdateOrdersTable()
        {
            client = new WCFServiceClient();
            try
            {
                client.Open();
                UpdateOrdersTable(client);
            }
            finally
            {
                client.Close();
            }
        }

        private void UpdateOrdersTable(WCFServiceClient client)
        {
			xOrders.DataContext = client.GetProductsOnOrderFullInfo();
        }
        #endregion

		#region Customers
		private void UpdateCustomersTable(WCFServiceClient client)
        {
			((CustomersViewPanel)(xCustomers.Content)).Customers = client.GetCustomers();
        }		
		#endregion

        private void xTabCtrl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(xTabCtrl.SelectedIndex == 2 &&
                xOrderViewPanel.ShoudUpdateDB)
            {
                xCustomersViewPanel.UpdatePanelInfo();
            }
        }
		#endregion
	}
}
